package com.takepoints21.mobile;

import com.takepoints.android.TPConst;
import com.takepoints.android.util.ShakeEventListener;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.provider.Settings;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class Takepoints extends BaseActivity {
    
	// http://androidthai.in.th/developer/165-creat-menu-application-android.html
	// http://androidthai.in.th/developer/167-mediaplayer-android-listview.html
	// http://stackoverflow.com/questions/11979388/android-possible-to-use-dp-on-a-layout-and-make-it-look-the-same-across-devices
	// http://developer.android.com/guide/practices/screens_support.html
	
	private GridView grid_main;
	private int densityDpi = 0;
	private int widthPixel = 0;
	private int heightPixel = 0;
	
	private SensorManager mSensorManager;
	private ShakeEventListener mSensorListener;
	
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //getWindow().setWindowAnimations(android.R.style.Animation_Translucent);

        densityDpi = getResources().getDisplayMetrics().densityDpi;
        //Display display = getWindowManager().getDefaultDisplay();
        widthPixel = getResources().getDisplayMetrics().widthPixels;
        heightPixel = getResources().getDisplayMetrics().heightPixels;
        
        // http://stackoverflow.com/questions/2317428/android-i-want-to-shake-it
        
		mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		mSensorListener = new ShakeEventListener();
        mSensorListener.setOnShakeListener(new ShakeEventListener.OnShakeListener() {
        	public void onShake() {
        		popup("Shakev!");
        	}
        });
        
        grid_main = (GridView)findViewById(R.id.gridMenu);
        //grid_main.setLayoutParams(new GridView.LayoutParams(LayoutParams.FILL_PARENT, getScreenHeight()-115));
        grid_main.setAdapter(new ImageAdapter(this));
        
        String uname = getSharedStringValue(TPConst.PRF_USRNAME);
        if(uname != null && uname.trim().length() > 0){
        	TextView lblHeader = (TextView)findViewById(R.id.lblHeader);
        	lblHeader.setText("");
        }
        manageMenu(1);
    }
    
    @Override
    protected void onResume() {
      super.onResume();
      mSensorManager.registerListener(mSensorListener,
          mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
          SensorManager.SENSOR_DELAY_UI);
    }

    @Override
    protected void onPause() {
      mSensorManager.unregisterListener(mSensorListener);
      super.onStop();
    }
    
    public class ImageAdapter extends BaseAdapter {
    	private Context mContext;
		public static final int ACTIVITY_CREATE = 10;
		
		private Integer[] mThumbIds = { R.drawable.menu12, R.drawable.menu22, R.drawable.menu42
									  , R.drawable.menu52, R.drawable.menu32/*, R.drawable.menu62*/
									  , R.drawable.menu72, R.drawable.menu82 };
		
		private Integer[] mThumbIdsFocus = { R.drawable.menu13, R.drawable.menu23, R.drawable.menu43
										  , R.drawable.menu53, R.drawable.menu33/*, R.drawable.menu63*/
										  , R.drawable.menu73, R.drawable.menu83 };
		
		public ImageAdapter(Context c){
			mContext = c;
		}
		
		@Override
		public int getCount(){
			return mThumbIds.length;
		}
		
		public View getView(final int position, View convertView, ViewGroup parent){
			 final ImageView imageView;
			 if(convertView == null){
				 imageView = new ImageView(mContext);
				 if(densityDpi >= 320){
					 imageView.setLayoutParams(new GridView.LayoutParams(195, 195));
				 } else if(densityDpi >= DisplayMetrics.DENSITY_HIGH){
					 if(heightPixel > 999){
						 imageView.setLayoutParams(new GridView.LayoutParams(175, 175));
					 } else{
						 imageView.setLayoutParams(new GridView.LayoutParams(130, 130));
					 }
				 } else if(densityDpi >= DisplayMetrics.DENSITY_MEDIUM){
					 imageView.setLayoutParams(new GridView.LayoutParams(87, 87));
				 } else{	// DisplayMetrics.DENSITY_LOW
					 imageView.setLayoutParams(new GridView.LayoutParams(52, 52));
				 }
				 imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
				 imageView.setPadding(2, 2, 2, 2);
			 } else{
				 imageView = (ImageView)convertView;
			 }
			 imageView.setImageResource(mThumbIds[position]);
			 imageView.setOnTouchListener(new View.OnTouchListener(){
				 public boolean onTouch(View v, MotionEvent event){
					 int action = event.getAction();
					 switch(action){
					 	case MotionEvent.ACTION_DOWN:
					 		imageView.setImageResource(mThumbIdsFocus[position]);
					 		break;
					 	case MotionEvent.ACTION_UP:
					 		imageView.setImageResource(mThumbIds[position]);
					 		break;
					 	case MotionEvent.ACTION_MOVE:
					 		imageView.setImageResource(mThumbIds[position]);
					 		break;
					 	case MotionEvent.ACTION_CANCEL:
					 		imageView.setImageResource(mThumbIds[position]);
					 		break;
					 	default:
					 		break;
					 }
					 return false;
				}
			 });
			 if(R.drawable.menu12 == mThumbIds[position]){
				 imageView.setOnClickListener(new View.OnClickListener(){
					 public void onClick(View view) {
						 Intent offersInt = new Intent(view.getContext(), TPOffers.class);
						 offersInt.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						 startActivityForResult(offersInt, 0);
					}
				 });
			 } else if(R.drawable.menu22 == mThumbIds[position]){
				 imageView.setOnClickListener(new View.OnClickListener(){
					 public void onClick(View view) {
						 Intent cardsInt = new Intent(view.getContext(), TPCards.class);
						 cardsInt.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						 startActivityForResult(cardsInt, 0);
					 }
				 });
			 } else if(R.drawable.menu32 == mThumbIds[position]){
				 imageView.setOnClickListener(new View.OnClickListener(){
					 public void onClick(View view) {
						Intent nearByInt = new Intent(view.getContext(), NearBy.class);
						nearByInt.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    	    			nearByInt.putExtra(TPConst.LOCATE_TYPE, TPConst.LOCATE_TYPE_NEAR_BY);
						startActivity(nearByInt);
					 }
				 });
			 } else if(R.drawable.menu42 == mThumbIds[position]){
				 imageView.setOnClickListener(new View.OnClickListener(){
					 public void onClick(View view) {
						 Intent rewardsInt = new Intent(view.getContext(), TPRewards.class);
						 rewardsInt.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						 startActivityForResult(rewardsInt, 0);
					 }
				 });
			 } else if(R.drawable.menu52 == mThumbIds[position]){
				 imageView.setOnClickListener(new View.OnClickListener(){
					 public void onClick(View view) {
						 if(getSharedIntValue(TPConst.PRF_MEMBER_ID) < 1){
							 confirmLoginDlg();
						 } else{
							 Intent myProfileInt = new Intent(view.getContext(), MyProfile.class);
							 myProfileInt.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
							 startActivityForResult(myProfileInt, 0);
						 }
					 }
				 });
			 } else if(R.drawable.menu62 == mThumbIds[position]){
				 imageView.setOnClickListener(new View.OnClickListener(){
					 public void onClick(View view) {
						 if(getSharedIntValue(TPConst.PRF_MEMBER_ID) < 1){
							 confirmLoginDlg();
						 } else{
							 // check in
						 }
					}
				 });
			 } else if(R.drawable.menu72 == mThumbIds[position]){
				 imageView.setOnClickListener(new View.OnClickListener(){
					 public void onClick(View view) {
						 Intent settingInt = new Intent(view.getContext(), Setting.class);
						 startActivityForResult(settingInt, 0);
					}
				 });
			 } else if(R.drawable.menu82 == mThumbIds[position]){
				 imageView.setOnClickListener(new View.OnClickListener(){
					 public void onClick(View view) {
						 popup("Takepoints.com");
					}
				 });
			 }
			 return imageView;
		}
		
		@Override
		public Object getItem(int arg0){
			return null;
		}
		
		@Override
		public long getItemId(int position){
			return position;
		}
	}
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event){
        if(keyCode == KeyEvent.KEYCODE_BACK){
            moveTaskToBack(true);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    
}
